
#include <scheduler.h>

typedef struct Request {
	// TODO: to-be-determined
} Request;

typedef struct Reply {
	// TODO: to-be-determined
} Reply;

void *rp_rindexd_scheduler(void *arg)
{
	static char fname[] = "rp_rindexd_schedular";
	static char msg[] = "fatal error";
	Err err;
	int fd;
	int tbc = 1;
	Request request;
	Reply reply;
	FLOG_IN(fname);

	while (tbc)		// TODO: How to break the loop upon shutdown/pause
	{
		/*	get the next client request   */
		err = rp_sem_wait(&(global.q_req.sem));	// all Scheduler threads block on this sem_wait
		fd = rp_queue_get(&(global.q_req));
		memset(&request, 0, sizeof(Request));
		memset(&reply, 0, sizeof(Reply));
		if (rp_comm_recv(fd, (void *)&request, sizeof(Request)) < sizeof(Request))
		{
			rp_log(LOG_ERROR, fname, "Cannot receive the incoming request from the client: IP: %s, Port: %d.");	// TODO:
			continue;
		}
		/*	search the b-tree, try to accquire the lock and apply the lease
			MORE DETAILS here
		*/
		rp_tree_search(BStree * tree, unsigned int key);		// client (rcached) is responsible for the filename hashing
		

		/*	return the result to the client    */
		rp_comm_send(fd, (void *)&reply, sizeof(Reply));
	}

	FLOG_OUT(fname);
	return NULL;
}

